package com.relytosoft.mizar.media.system.services.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.BeanCopyUtils;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.dubhe.common.log.annotation.OpLog;
import com.relytosoft.dubhe.common.log.enums.OpTypeEnum;
import com.relytosoft.mizar.media.system.api.entity.SysSecurityLevel;
import com.relytosoft.mizar.media.system.api.pojo.dto.SysSecurityLevelDto;
import com.relytosoft.mizar.media.system.services.service.SysDistrictService;
import com.relytosoft.mizar.media.system.services.service.SysSecurityLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 安全级别
 *
 * @Author SunLi
 * @Date: 2022/4/6 13:46
 * @Version 1.0
 */

@Validated
@RestController
@RequestMapping("/system/securitylevel")
@Api(tags = "安全级别相关接口")
public class SysSecurityLevelController {
    @Resource
    private SysSecurityLevelService sysSecurityLevelService;

    @Resource
    private SysDistrictService sysDistrictService;

    @GetMapping(value = "/pagelist")
    @SaCheckPermission("system:securitylevel:list")
    @ApiOperation("获得安全级别列表分页信息")
    public ResponseResult<Map<String, Object>> selectSysSecurityLevelPageList(PageQuery pageQuery, SysSecurityLevelDto sysSecurityLevel) {
        SysSecurityLevel entity = BeanCopyUtils.copy(sysSecurityLevel, SysSecurityLevel.class);
        return ResponseResult.ok(CommonUtils.getPageData(sysSecurityLevelService.selectSysSecurityLevelPageList(pageQuery, entity)));
    }

    @GetMapping(value = "/list")
    @SaCheckPermission("system:securitylevel:list")
    @ApiOperation("获得已启用的安全级别列表")
    public ResponseResult<List<SysSecurityLevel>> selectSysSecurityLevelList() {
        LambdaQueryWrapper<SysSecurityLevel> lw = Wrappers.lambdaQuery();
        lw.eq(SysSecurityLevel::getStatus, 1);
        return ResponseResult.ok(sysSecurityLevelService.list(lw));
    }

    @PostMapping(value = "/insert")
    @SaCheckPermission("system:securitylevel:insert")
    @ApiOperation("添加安全级别信息")
    @OpLog(msg = "添加安全级别信息", opType = OpTypeEnum.INSERT)
    public ResponseResult<Void> insertSysSecurityLevel(@Valid @RequestBody SysSecurityLevelDto sysSecurityLevel) {
        SysSecurityLevel entity = BeanCopyUtils.copy(sysSecurityLevel, SysSecurityLevel.class);
        String mes = validate(entity);
        if (!StrUtil.isEmpty(mes)) {
            return ResponseResult.fail(mes);
        }
        entity.setId(null);
        entity.setStatus(1);
        boolean result = sysSecurityLevelService.save(entity);
        if (result) {
            return ResponseResult.ok();
        }
        return ResponseResult.fail();
    }

    @PutMapping(value = "/update")
    @SaCheckPermission("system:securitylevel:update")
    @ApiOperation("修改安全级别信息")
    @OpLog(msg = "修改安全级别信息", opType = OpTypeEnum.UPDATE)
    public ResponseResult<Void> updateSysSecurityLevel(@Valid @RequestBody SysSecurityLevelDto sysSecurityLevel) {
        SysSecurityLevel entity = BeanCopyUtils.copy(sysSecurityLevel, SysSecurityLevel.class);
        String mes = validate(entity);
        if (!StrUtil.isEmpty(mes)) {
            return ResponseResult.fail(mes);
        }
        boolean result = sysSecurityLevelService.updateById(entity);
        if (result) {
            return ResponseResult.ok();
        }
        return ResponseResult.fail();
    }

    @PutMapping(value = "/updatestatus/{id}/{status}")
    @SaCheckPermission("system:securitylevel:updatestatus")
    @ApiOperation("修改安全级别信息状态")
    @OpLog(msg = "修改安全级别信息状态", opType = OpTypeEnum.SET)
    public ResponseResult<Void> setSysSecurityLevelStatus(@PathVariable int id, @PathVariable int status) {
        //禁用安全级别时，判断是否已被区域使用
        if (status == 0 && sysDistrictService.countBySecurityLevel(id) > 0) {
            return ResponseResult.fail("安全级别已被区域使用！");
        }
        int result = sysSecurityLevelService.updateSysSecurityLevelStatus(id, status);
        if (result > 0) {
            return ResponseResult.ok();
        }
        return ResponseResult.fail();
    }

    @GetMapping(value = "/{id}")
    @SaCheckPermission("system:securitylevel:getinfo")
    @ApiOperation("获得安全级别信息")
    public ResponseResult<SysSecurityLevel> getSysSecurityLevel(@PathVariable int id) {
        return ResponseResult.ok(sysSecurityLevelService.getById(id));
    }

    private String validate(SysSecurityLevel entity) {
        if (StrUtil.isEmpty(entity.getName())) {
            return "安全级别名称不能为空！";
        }
        if (StrUtil.isEmpty(entity.getColor())) {
            return "安全级别颜色不能为空！";
        }
        if(!sysSecurityLevelService.checkSecurityLevelNameUnique(entity)){
            return "安全级别名称已存在！";
        }
        return "";
    }
}
